package cn.fl.insurance.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.comm.facade.upload.dto.SysUlFilesRecDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.*;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsCompInfoQueryCondition;
import cn.fl.insurance.condition.InsPlcApprQueryCondition;
import cn.fl.insurance.constant.InsuranceConstant;
import cn.fl.insurance.dto.InsCompInfoDTO;
import cn.fl.insurance.dto.InsPlcApprDTO;
import cn.fl.insurance.service.*;
import cn.fl.insurance.transform.InsPlcApprVOTransform;
import cn.fl.insurance.vo.InsPlcApprVO;
import cn.fl.insurance.vo.InsPlcBscInfoVO;
import cn.fl.insurance.vo.InsPlcTypDtlVO;
import cn.fl.preloan.dto.insurance.AddInsPlcApprRequest;
import cn.fl.preloan.dto.insurance.GetInsPlcApprByPlcIdRequest;
import cn.fl.preloan.dto.insurance.GetInsPlcBscInfoRequest;
import cn.fl.preloan.dto.insurance.InsPlcBscInfoDTO;
import cn.fl.project.condition.PrjPrdDtlForInsQueryCondition;
import cn.fl.project.dto.PrjPrdDtlForInsDTO;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.transform.SysUlFilesRecVOTransform;
import cn.fl.system.vo.SysUlFilesRecVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.fl.commons.result.ResponseData;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 批单表 控制层
 */
@Controller
@RequestMapping("/insurance/insPlcAppr")
public class InsPlcApprController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(InsPlcApprController.class);

    /**
     * InsPlcAppr 服务层BEAN
     */
    @Autowired
    private IInsPlcApprService insPlcApprService;
    /**
     * 数据字典 服务层BEAN
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    /**
     * InsPlcBscInfo 服务层BEAN
     */
    @Autowired
    private IInsPlcBscInfoService insPlcBscInfoService;
    /**
     * InsCompInfo 服务层BEAN
     */
    @Autowired
    private IInsCompInfoService insCompInfoService;
    /**
     * InsTypInfo 服务层BEAN
     */
    @Autowired
    private IInsTypInfoService insTypInfoService;
    /**
     * InsPlcPrdDtlTk 服务层BEAN
     */
    @Autowired
    private IInsPlcPrdDtlTkService insPlcPrdDtlTkService;
    /**
     * 贷前保单服务
     */
    @Autowired
    private cn.fl.preloan.IInsPlcBscInfoService iInsPlcBscInfoService;
    /**
     * 贷前批单服务
     */
    @Autowired
    private cn.fl.preloan.IInsPlcApprService iInsPlcApprService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载InsPlcAppr列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insPlcAppr/insPlcApprList");
        return mav;
    }

    /**
     * 到新增页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toAddPage", "加载InsPlcAppr Add页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insPlcAppr/insPlcApprModify");
        //TODO 加载数据字典数据、传值等 ？

        return mav;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toModifyPage", "加载InsPlcAppr insPlcApprModify页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insPlcAppr/insPlcApprModify");
        String id = request.getParameter("id");
        //InsPlcBscInfoVO insPlcBscInfoVO = this.insPlcBscInfoService.selectById(Long.valueOf(id));
        GetInsPlcBscInfoRequest getRequest = new GetInsPlcBscInfoRequest();
        getRequest.setId(Long.valueOf(id));
        ResponseData responseData = iInsPlcBscInfoService.selectById(getRequest);
        InsPlcBscInfoDTO insPlcBscInfoDTO = (InsPlcBscInfoDTO) responseData.getData();
        InsPlcBscInfoVO insPlcBscInfoVO = new InsPlcBscInfoVO();
        BeanUtils.copyProperties(insPlcBscInfoDTO,insPlcBscInfoVO);
        /*InsPlcBscInfoVO insPlcBscInfoVO = this.insPlcBscInfoService.selectById(Long.valueOf(id));*/
        if (insPlcBscInfoVO.getPlcBgTm() != null) {
            insPlcBscInfoVO.setPlcBgTms(DateUtils.formatDateToString(insPlcBscInfoVO.getPlcBgTm(), DateUtils.YYYY_MM_DD));
        }
        if (insPlcBscInfoVO.getPlcEndTm() != null) {
            insPlcBscInfoVO.setPlcEndTms(DateUtils.formatDateToString(insPlcBscInfoVO.getPlcEndTm(), DateUtils.YYYY_MM_DD));
        }
        if(insPlcBscInfoVO.getInsTtlFee()!=null){
            insPlcBscInfoVO.setInsTtlFeeDb(DecimalUtils.convertFenToYuan(insPlcBscInfoVO.getInsTtlFee()));
        }
        mav.addObject("insPlcBscInfoVO", insPlcBscInfoVO);
        mav.addObject("paySchId", insPlcBscInfoVO.getPaySchId());//支付表ID
        mav.addObject("paySchNo", insPlcBscInfoVO.getPaySchNo());//支付表号
        mav.addObject("prjPrdDtlId", insPlcBscInfoVO.getPrjPrdDtlId());//项目产品明细ID

        //查询保单产品信息
        /*InsPlcPrdDtlTkQueryCondition prdDtlQueryCondition=new InsPlcPrdDtlTkQueryCondition();
        prdDtlQueryCondition.setInsPlcId(Long.valueOf(id));
        List<InsPlcPrdDtlTkVO> prdList=insPlcPrdDtlTkService.searchList(prdDtlQueryCondition);
        mav.addObject("prdList",JSON.toJSONString(prdList));*/

        //查询上次批单记录
        //InsPlcApprDTO insPlcApprDTO=this.insPlcApprService.selectTopOneByPlcId(Long.valueOf(id));
        //引入贷前服务
        GetInsPlcApprByPlcIdRequest plcIdRequest = new GetInsPlcApprByPlcIdRequest();
        plcIdRequest.setPlcId(Long.valueOf(id));
        ResponseData data = iInsPlcApprService.selectTopOneByPlcId(plcIdRequest);
        cn.fl.preloan.dto.insurance.InsPlcApprDTO insPlcApprDTO = (cn.fl.preloan.dto.insurance.InsPlcApprDTO) data.getData();
        /*InsPlcApprDTO insPlcApprDTO=this.insPlcApprService.selectTopOneByPlcId(Long.valueOf(id));*/
        InsPlcApprVO vo = new InsPlcApprVO();
        if(insPlcApprDTO != null){
            BeanUtils.copyProperties(insPlcApprDTO,vo);
            //InsPlcApprVO vo = InsPlcApprVOTransform.toVO(insPlcApprDTO);
            if (insPlcApprDTO.getPlcBgTm() != null) {
                vo.setPlcBgTms(DateUtils.formatDateToString(insPlcBscInfoVO.getPlcBgTm(), DateUtils.YYYY_MM_DD));
            }
            if (insPlcApprDTO.getPlcEndTm() != null) {
                vo.setPlcEndTms(DateUtils.formatDateToString(insPlcBscInfoVO.getPlcEndTm(), DateUtils.YYYY_MM_DD));
            }
            if(insPlcBscInfoVO.getInsTtlFee()!=null){
                vo.setInsTtlFeeDb(DecimalUtils.convertFenToYuan(vo.getInsTtlFee()));
            }
            mav.addObject("insPlcApprVo", vo);
        }
        this.prepare(request, mav);

        if(StringUtils.isNotEmpty(insPlcBscInfoVO.getPrjId())){
            mav.addObject("prjId",insPlcBscInfoVO.getPrjId());
            PrjBscInfoVO prjBscInfoVO=this.insPlcBscInfoService.searchPrjBscInfo(insPlcBscInfoVO.getPrjId());
            if(null != prjBscInfoVO){
                mav.addObject("prjTypCd",prjBscInfoVO.getPrjTypCd());
                mav.addObject("bizTypCd",prjBscInfoVO.getBizTypCd());
                //mav.addObject("rgnCd","0,"+prjBscInfoVO.getRgnPrCd()+","+prjBscInfoVO.getRgnCyCd());
                mav.addObject("orgCd",prjBscInfoVO.getBuOrgCd());
                mav.addObject("buOrgCd",prjBscInfoVO.getBuOrgCd());

                //暂时处理一下
                if(InsuranceConstant.INS_PRJ_BU_ORG_CD_5.equals(prjBscInfoVO.getBuOrgCd())){
                    mav.setViewName("insurance/insPlcAppr/insPlcApprAdd");
                }else{
                    //查询项目产品明细信息  (重复查询 待优化)
                    PrjPrdDtlForInsQueryCondition qc = new PrjPrdDtlForInsQueryCondition();
                    qc.setId(insPlcBscInfoVO.getPrjPrdDtlId());
                    qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
                    qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
                    List<PrjPrdDtlForInsDTO> prdList = this.insPlcBscInfoService.searchInsurePrdList(qc);

                    //项目产品类型 ：挂车标示 1.挂车 0.非挂车
                    mav.addObject("isTrailer",BaseConstant.IS_YESNO_NO);
                    if(CollectionUtils.isNotEmpty(prdList)){
                        PrjPrdDtlForInsDTO prjPrdDtlForInsDTO=prdList.get(0);
                        mav.addObject("actPrdId", prjPrdDtlForInsDTO.getActPrdId());//车辆实际ID

                        String prdTypCd = prjPrdDtlForInsDTO.getPrdTypCd();
                        if(StringUtils.isNotEmpty(prdTypCd)){
                            if(prdTypCd.startsWith(InsuranceConstant.CODE_PRD_TYP_CD_006)||prdTypCd.startsWith(InsuranceConstant.CODE_PRD_TYP_CD_014)
                                    ||prdTypCd.startsWith(InsuranceConstant.CODE_PRD_TYP_CD_015)){
                                //挂车 不用上传交强险保单
                                mav.addObject("isTrailer",BaseConstant.IS_YESNO_YES);
                            }
                        }
                    }
                }
            }
        }
        mav.addObject("ctrlKey","1");//控制保单附件维护 1.可上传删除 2.审核
        return mav;
    }

    private void prepare(HttpServletRequest request,ModelAndView mav){
        //加载数据字典数据、传值等 ？
        /*InsTypInfoQueryCondition condition=new InsTypInfoQueryCondition();
        condition.setTypLvl(InsuranceConstant.CODE_INS_TYP_LVL_CD);//保险父类
        List<KeyValueVO> insTypCdList=this.insTypInfoService.searchInsTypCdList(condition);//保险种类*/

        List<KeyValueVO> insTypCdList=this.insPlcBscInfoService.searchInsTypCdList();//保险种类
        List<KeyValueVO> buyWayCdList=this.insPlcBscInfoService.searchBuyWayCdList();//购买方式
        List<KeyValueVO> plcExpCdList=this.insPlcBscInfoService.searchPlcExpCdList();//保单期限
        List<KeyValueVO> plcPropCdList=this.insPlcBscInfoService.searchPlcPropCdList();//保单性质
        List<KeyValueVO> prdTypCdList=this.insPlcBscInfoService.searchPrdTypCdList();//保单产品类型
        //List<KeyValueVO> plcSpePolCdList=this.insPlcBscInfoService.searchPlcSpePolCdList();//保险特殊渠道政策
        mav.addObject("insTypCdList", JSON.toJSONString(insTypCdList));
        mav.addObject("buyWayCdList", JSON.toJSONString(buyWayCdList));
        mav.addObject("plcExpCdList", JSON.toJSONString(plcExpCdList));
        mav.addObject("plcPropCdList", JSON.toJSONString(plcPropCdList));
        mav.addObject("prdTypCdList", JSON.toJSONString(prdTypCdList));
        //mav.addObject("plcSpePolCdList", JSON.toJSONString(plcSpePolCdList));

        //保险公司
        InsCompInfoQueryCondition qc = new InsCompInfoQueryCondition();
        qc.setCompLvl(1);
        List<InsCompInfoDTO> insCompList = this.insCompInfoService.searchList(qc);
        if (CollectionUtils.isNotEmpty(insCompList)) {
            List<KeyValueVO> compList=new ArrayList<>(insCompList.size());
            KeyValueVO vo=null;
            for(InsCompInfoDTO dto:insCompList){
                vo=new KeyValueVO();
                vo.setKey(dto.getId().toString());
                vo.setValue(dto.getCompNm());
                compList.add(vo);
            }
            //mav.addObject("insCompList", JSON.toJSONString(InsCompInfoVOTransform.toVOList(insCompList)));
            mav.addObject("insCompList", JSON.toJSONString(compList));
        }
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initInsPlcApprList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(currentPage)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        InsPlcApprQueryCondition qc = new InsPlcApprQueryCondition();
        //TODO 设置查询条件？
        String searchName = request.getParameter("demo.name");
        if (StringUtils.isNotEmpty(searchName)) {

        }
        page.setCondition(qc);

        BaseLogUtils.info(logger, "initInsPlcApprList", "初始化InsPlcAppr列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.insPlcApprService.searchListPage(page);
        List<InsPlcApprVO> list = (List<InsPlcApprVO>) pager.getResultList();
        ListPageVO<InsPlcApprVO> listPage = new ListPageVO<InsPlcApprVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeInsPlcApprByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "removeInsPlcApprByIds", "删除InsPlcAppr数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }

        try {
            boolean success = this.insPlcApprService.removeByIds(idsList);
            if (!success) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
                BaseLogUtils.info(logger, "removeInsPlcApprByIds", "删除InsPlcAppr数据失败,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
            }
            BaseLogUtils.info(logger, "removeInsPlcApprByIds", "删除InsPlcAppr数据成功,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "removeInsPlcApprByIds", "删除InsPlcAppr数据失败,ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "removeInsPlcApprByIds", "删除InsPlcAppr数据失败,系统错误.ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getInsPlcApprById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "getInsPlcApprById", "获取InsPlcAppr数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            InsPlcApprVO vo = this.insPlcApprService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getInsPlcApprById", "获取InsPlcAppr数据不存在,ID:" + id, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(vo);
            BaseLogUtils.info(logger, "getInsPlcApprById", "获取InsPlcAppr数据成功,ID:" + id, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "getInsPlcApprById", "获取InsPlcAppr数据失败,ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getInsPlcApprById", "获取InsPlcAppr数据失败,系统错误.ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addInsPlcAppr(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "批单成功");
        InsPlcBscInfoVO entity = null;//获取参数
        try {
            entity = this.getRequestParamForInsPlcAppr(request);//获取参数
            //this.insPlcApprService.add(entity);
            //BaseLogUtils.info(logger, "addInsPlcAppr", "新增InsPlcAppr数据成功.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            AddInsPlcApprRequest addRequest = new AddInsPlcApprRequest();
            InsPlcBscInfoDTO insPlcBscInfoDTO = new InsPlcBscInfoDTO();
            BeanUtils.copyProperties(entity,insPlcBscInfoDTO);
            addRequest.setDto(insPlcBscInfoDTO);
            List<SysUlFilesRecDTO> sysUlFilesRecDTOS = SysUlFilesRecVOTransform.toDTOList(entity.getSysUlFilesRecVOList());
            addRequest.setSysUlFilesRecDTOList(sysUlFilesRecDTOS);
            ResponseData responseData = iInsPlcApprService.addInsPlcAppr(addRequest);
            int temp = (int) responseData.getData();
            result.setData(temp);
            /*this.insPlcApprService.add(entity);*/
            BaseLogUtils.info(logger, "addInsPlcAppr", "贷前--新增InsPlcAppr,request:" + JSONObject.toJSONString(addRequest)
                    + ",response: " + temp, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addInsPlcAppr", "新增InsPlcAppr数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addInsPlcAppr", "新增InsPlcAppr数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 验证批单号是否唯一
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/validateApprCode.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object validateApprCode(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "查询成功");
        String apprCode=request.getParameter("apprCode");//保单号
        if(StringUtils.isEmpty(apprCode)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("查询失败，批单号为空");
            return result;
        }
        BaseLogUtils.info(logger,"validateApprCode","验证批单号是否唯一："+apprCode,CurrentThreadContext.getCurrentUserName());
        result.setData(this.insPlcApprService.validateApprCode(apprCode));
        return result;
    }

    /**
     * 获取请求参数
     *
     * @param request 请求
     * @return
     */
    private InsPlcBscInfoVO getRequestParamForInsPlcAppr(HttpServletRequest request) throws BaseException {
        //获取参数
        String id = request.getParameter("id");//主键
        String plcNo = request.getParameter("plcNo");
        String insTypCd = request.getParameter("insTypCd");
        String insCompId = request.getParameter("insCompId");
        String insCompSubId = request.getParameter("insCompSubId");
        String insCompSubNm = request.getParameter("insCompSubNm");
        String buyWayCd = request.getParameter("buyWayCd");
        String insPerNm = request.getParameter("insPerNm");
        String insPerCretNo = request.getParameter("insPerCretNo");
        String insTtlFee = request.getParameter("insTtlFee");
        String plcBgTm = request.getParameter("plcBgTm");
        String plcEndTm = request.getParameter("plcEndTm");
        String plcExpCd = request.getParameter("plcExpCd");
        String plcPropCd = request.getParameter("plcPropCd");
        String plcRem = request.getParameter("plcRem");
        String prdTypCd = request.getParameter("prdTypCd");
        String fileParams = request.getParameter("fileParams");//附件上传信息
        String apprCode = request.getParameter("apprCode");

        //验证参数并赋值
        InsPlcBscInfoVO entity = new InsPlcBscInfoVO();
        entity.setPlcNo(plcNo);
        entity.setInsTypCd(insTypCd);
        entity.setPrdTypCd(prdTypCd);
        if (StringUtils.isBlank(insCompId)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "保险公司不能为空.");
        }
        entity.setInsCompId(Long.valueOf(insCompId));
        if (StringUtils.isNotBlank(insCompSubId)) {
            entity.setInsCompSubId(Long.valueOf(insCompSubId));
        }
        entity.setInsCompSubNm(insCompSubNm);
        if(StringUtils.isBlank(apprCode)){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "批单号不能为空.");
        }
        entity.setApprCode(apprCode);

        //解析附件上传信息
        if (StringUtils.isNotBlank(fileParams)) {
            List<Map<String, String>> fileParamsList = new ArrayList<Map<String, String>>();
            JSONArray arry = JSONArray.fromObject(fileParams);
            for (int i = 0; i < arry.size(); i++) {
                net.sf.json.JSONObject jsonObject = arry.getJSONObject(i);
                Iterator<?> it = jsonObject.keys();
                HashMap<String, String> hMap = new HashMap<String, String>();
                while (it.hasNext()) {
                    String key = String.valueOf(it.next());
                    String value = (String) jsonObject.get(key);
                    hMap.put(key, value);
                }
                fileParamsList.add(hMap);
            }
            List<SysUlFilesRecVO> sysUlFilesRecVOList = new ArrayList<SysUlFilesRecVO>();
            String rootPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH);
            for (Map<String, String> map : fileParamsList) {
                SysUlFilesRecVO sysUlFilesRecVO = new SysUlFilesRecVO();
                sysUlFilesRecVO.setFileNm(map.get("fileNm"));
                sysUlFilesRecVO.setFilePath(map.get("filePath"));
                sysUlFilesRecVO.setOrgCd("0000");//默认不区分
                sysUlFilesRecVO.setSysCd(CurrentThreadContext.getCurrentSysCd());
                sysUlFilesRecVO.setCfgCd(map.get("cfgCd"));
                sysUlFilesRecVO.setCfgDtlCd(map.get("cfgDtlCd"));
                sysUlFilesRecVO.setCrtUsrRlNm(CurrentThreadContext.getCurrentUserName());
                sysUlFilesRecVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                sysUlFilesRecVO.setCrtTm(new Date());
                sysUlFilesRecVO.setAbsLocPath(rootPath+sysUlFilesRecVO.getFilePath());
                sysUlFilesRecVOList.add(sysUlFilesRecVO);
            }
            entity.setSysUlFilesRecVOList(sysUlFilesRecVOList);
        }

        entity.setBuyWayCd(buyWayCd);
        entity.setPlcPropCd(plcPropCd);
        entity.setPlcExpCd(plcExpCd);
        if(StringUtils.isNotBlank(plcBgTm)){
            entity.setPlcBgTm(DateUtils.formatStringToDate(plcBgTm, DateUtils.YYYY_MM_DD));
        }
        if(StringUtils.isNotBlank(plcBgTm)) {
            entity.setPlcEndTm(DateUtils.formatStringToDate(plcEndTm, DateUtils.YYYY_MM_DD));
        }
        entity.setInsPerNm(insPerNm);
        entity.setInsPerCretNo(insPerCretNo);
        entity.setPlcRem(plcRem);
        if (StringUtils.isNotEmpty(insTtlFee)) {
            Double ttlFee = Double.valueOf(insTtlFee) * 100;
            entity.setInsTtlFee(ttlFee.longValue());
        }

        //保单产品信息

        //保单险种信息
        String insTyps = request.getParameter("insTyp");
        if (StringUtils.isNotBlank(insTyps)) {
            List<InsPlcTypDtlVO> insPlcTypDtlVOList = JSON.parseArray(insTyps, InsPlcTypDtlVO.class);
            //验证险种唯一
            StringBuffer strbuf = new StringBuffer();
            for (InsPlcTypDtlVO vo : insPlcTypDtlVOList) {
                if (strbuf.toString().contains(vo.getInsTypId().toString())) {
                    throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "保单险种重复.");
                }
                strbuf.append(vo.getInsTypId() + ";");

                if (vo.getInsAmtDb() == null) {
                    vo.setInsAmt(0l);
                } else {
                    vo.setInsAmtDb(vo.getInsAmtDb() * 100);
                    vo.setInsAmt(vo.getInsAmtDb().longValue());
                }
                if (vo.getInsFeeDb() == null) {
                    vo.setInsFee(0l);
                } else {
                    vo.setInsFeeDb(vo.getInsFeeDb() * 100);
                    vo.setInsFee(vo.getInsFeeDb().longValue());
                }

                if (StringUtils.isEmpty(vo.getIsNonDedu())) {//不计免赔 默认是
                    vo.setIsNonDedu(1);
                }
            }
            entity.setInsPlcTypDtlVOList(insPlcTypDtlVOList);
        }

        Date tm = new Date();
        if (StringUtils.isNotBlank(id)) {
            entity.setId(Long.valueOf(id));
            entity.setMdfTm(tm);
            entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
        } else {
            entity.setCrtTm(tm);
            entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        }
        return entity;
    }
}

